﻿using System;
using System.Timers;


    class C5
    {
        static void Main()
        {
            var alarmClock = new AlarmClock();

           
            alarmClock.Tick += (s, e) => Console.WriteLine($"TICK... 当前时间：{DateTime.Now:HH:mm:ss}");
            alarmClock.Alarm += (s, e) => Console.WriteLine("time to Alarm");

            Console.Write("请输入闹钟小时（0-23）：");
            int hour = int.Parse(Console.ReadLine());
            Console.Write("请输入闹钟分钟（0-59）：");
            int minute = int.Parse(Console.ReadLine());

            alarmClock.SetAlarm(hour, minute);
            Console.WriteLine($"闹钟已设置到 {hour:00}:{minute:00}");
            Console.ReadKey();
        }
    }

  
    class AlarmClock
    {
        private readonly System.Timers.Timer _timer;
        private DateTime _alarmTime;

       
        //两种事件
        public event EventHandler Tick;
        public event EventHandler Alarm;

        public AlarmClock()
        {
            _timer = new System.Timers.Timer(1000); 
            _timer.Elapsed += TimerElapsed;
        }

       
        public void SetAlarm(int hour, int minute)
        {
            _alarmTime = DateTime.Today.AddHours(hour).AddMinutes(minute);

            
            if (_alarmTime < DateTime.Now)
            {
                _alarmTime = _alarmTime.AddDays(1);
            }

            _timer.Start();
        }

       
        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            
            Tick?.Invoke(this, EventArgs.Empty);

           
            if (DateTime.Now >= _alarmTime)
            {
                // 触发Alarm
                Alarm?.Invoke(this, EventArgs.Empty);
                _timer.Stop();
            }
        }
    }

